package com.smy.jkds.controller;


import com.smy.jkds.po.*;
import com.smy.jkds.service.TTaskRecordService;
import com.smy.jkds.vo.IdListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 任务记录表 前端控制器
 * </p>
 *
 * @author 01140
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/jkds")
public class TTaskRecordController {

    @Autowired
    TTaskRecordService tTaskRecordService;


    @PostMapping("/createTask")
    public Result createTask(@RequestBody TTaskRecord taskRecord, @CurrentUser JkdsUser user){
        taskRecord.setUserId(user.getId());
        taskRecord.setUserName(user.getName());
        if (taskRecord.getCreateDate() == null){
            taskRecord.setCreateDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        }
        boolean r = tTaskRecordService.save(taskRecord);
        return r ? Result.success("新增成功") : Result.error("新增失败");
    }

    @PostMapping("/updateTask")
    public Result updateTask(@RequestBody TTaskRecord taskRecord){
        boolean r = tTaskRecordService.updateById(taskRecord);
        return r ? Result.success("更新成功") : Result.success("更新失败");
    }

    @PostMapping("/deleteTask")
    public Result deleteCard(@RequestBody IdListVO cardDeleteVO, @CurrentUser JkdsUser user){
        if (cardDeleteVO == null || cardDeleteVO.getIdList() == null){
            return Result.success("更新成功");
        }
//        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in(Card.ColumnEnum.id.name(), cardDeleteVO.getIdList());
        boolean r = tTaskRecordService.removeByIds(cardDeleteVO.getIdList());
        return r ? Result.success("删除成功") : Result.success("删除失败");
    }

    @GetMapping("/queryTaskList")
    public Result queryTaskList(@CurrentUser JkdsUser loginUser, String createDate){
        return Result.success("查询成功",tTaskRecordService.queryTaskRecoordList(loginUser.getId(), createDate));
    }

    @GetMapping("/queryTaskById")
    public Result queryTaskById(@CurrentUser JkdsUser loginUser, String id){
        return Result.success("查询成功",tTaskRecordService.taskVOList(loginUser,null, id));
    }

    @GetMapping("/queryUserSpaceTaskList")
    public Result queryUserSpaceTaskList(@CurrentUser JkdsUser loginUser, String createDate){
        Map<String, Object> map = new HashMap<>();
        map.put("taskVOList", tTaskRecordService.taskVOList(loginUser, createDate, null));
        return Result.success("查询成功",map);
    }
}

